package com.goldenautumn.prefabricate.activity.mine.adapter

import android.content.Context
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.AddressInfoVo
import com.goldenautumn.prefabricate.databinding.ItemAddressBinding
import qncx.external.common.compat.isVisible

class AddressAdapter  : BaseQuickAdapter<AddressInfoVo, DataBindingHolder<ItemAddressBinding>>() {

    var selectPosition = -1

    override fun onBindViewHolder(holder: DataBindingHolder<ItemAddressBinding>, position: Int, item: AddressInfoVo?) {
        if(item == null) return
        var mBinding  = holder.binding
        var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        when (holder.layoutPosition) {
            items.size - 1 -> params.setMargins(0, SizeUtils.dp2px(12f), 0, SizeUtils.dp2px(12f))
            else -> params.setMargins(0, SizeUtils.dp2px(8f), 0, 0)
        }
        holder.itemView.layoutParams = params

        mBinding.addressCheck.isVisible(selectPosition >= 0 && "${item.address_id}" == "$selectPosition")
        mBinding.defaults.isVisible(item.isDefault == true)
        mBinding.selectBtn.setImageResource(if(item.isDefault == true) R.mipmap.ic_address_select else R.mipmap.ic_address_not_select)

        mBinding.selectBtn.isVisible()

        mBinding.nickName.text = "${item.name}"
        mBinding.phone.text = "${item.phone}"
        mBinding.address.text = "${item.detail}"
        mBinding.city.text = "${item.region?.province},${item.region?.city},${item.region?.region}"

    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemAddressBinding> {
        return DataBindingHolder(R.layout.item_address,parent)
    }

}